package com.spirng.leetcode.day06;

import java.util.ArrayList;
import java.util.List;

public class Test257 {
    public List<String> binaryTreePaths(TreeNode root) {
        ArrayList<Integer> paths = new ArrayList<>();
        ArrayList<String> res = new ArrayList<>();
        binary(root,paths,res);
        return res;
    }
    public void binary(TreeNode node,List<Integer> paths, List<String> res){
        paths.add(node.val);
        if(node.left==null&&node.right==null){
            StringBuilder str=new StringBuilder();
            for (int i = 0; i < paths.size() - 1; i++) {
                str.append(paths.get(i)).append("->");
            }
            str.append(paths.get(paths.size()-1));
            res.add(new String(str));
            return ;
        }
        if(node.left!=null){
            binary(node.left,paths,res);
            paths.remove(paths.size()-1);//回溯返回上一个分叉节点
        }
        if(node.right!=null) {
            binary(node.right, paths, res);
            paths.remove(paths.size()-1);
        }
    }
}
